home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1994 August: Tool Chest / Dev.CD Aug 94.toast / New System Software Extensions / OpenDoc A6 / OpenDoc Parts Framework / OPF / Found / FWExcLib / Include / FWNewHel.h < prev    next >
Encoding:
C/C++ Source or Header  |  1994-04-21  |  5.9 KB  |  183 lines  |  [TEXT/MPS ]

  1. #ifndef FWNEWHEL_H
  2. #define FWNEWHEL_H
  3. //========================================================================================
  4. //
  5. //    File:                FWNewHel.h
  6. //    Release Version:    $ 1.0d1 $
  7. //
  8. //    Creation Date:        3/28/94
  9. //
  10. //    Copyright:    © 1994 by Apple Computer, Inc., all rights reserved.
  11. //
  12. //========================================================================================
  13.  
  14. #ifndef   FWPRIDEB_H
  15. #include "FWPriDeb.h"
  16. #endif
  17.  
  18. #ifndef   FWAUTODE_H
  19. #include "FWAutoDe.h"
  20. #endif
  21.  
  22. #ifndef   FWEXCTAS_H
  23. #include "FWExcTas.h"
  24. #endif
  25.  
  26. //========================================================================================
  27. //    CLASS _FW_CPrivSubHelper
  28. //========================================================================================
  29.  
  30. class _FW_CPrivSubHelper
  31. {
  32. public:
  33.     
  34.     static void InitializeStorageArray(FW_SPrivExceptionGlobals& globals,
  35.                                     _FW_CPrivSubHelper*array, int size);
  36.  
  37. private:
  38.  
  39.     friend class _FW_CPrivNewHelper;
  40.         // _FW_CPrivSubHelper should be a nested class of _FW_CPrivNewHelper
  41.  
  42.     void* operator new(size_t size);
  43.     void  operator delete(void* object);
  44.     
  45.     _FW_CPrivSubHelper(_FW_CAutoDestructObject *subObject, 
  46.                        size_t subObjectSize,
  47.                        _FW_CPrivSubHelper    *nextSubHelper);
  48.     ~_FW_CPrivSubHelper();
  49.     
  50.     _FW_CAutoDestructObject *fSubObject;
  51.         // The subobject being constructed.
  52.     
  53.     size_t fSubObjectSize;
  54.         // The size of the subobject being watched.
  55.         // This information is necessary for tracking nested subobjects.
  56.         
  57.     void *fSubObjectVTable;
  58.         // The objects vtable at time of last completed constructor
  59.         
  60.     _FW_CPrivSubHelper    *fSiblings;
  61.         // Next link for subobject peer to this one.
  62. };
  63.  
  64. //========================================================================================
  65. //    CLASS _FW_CPrivNewHelper
  66. //
  67. //         This helper object watches a dynamically allocated object until it is fully 
  68. //        constructed. If an exception is thrown before the object is fully constructed,
  69. //        this helper will delete the object.
  70. //========================================================================================
  71.  
  72. class _FW_CPrivNewHelper : public _FW_CAutoDestructObject
  73. {
  74.  
  75. public:
  76.  
  77.     friend class _FW_CAutoDestructObject;
  78.  
  79.     enum
  80.     {
  81.         kMaxNewHelperStackItems = 100,
  82.             // Maximum items on NewHelper stack.
  83.             // This limits 'nested news', i.e. objects that are newed that have
  84.             //   constructors that new objects that have constructors that new objects...
  85.         kNewHelperStackSize = kMaxNewHelperStackItems*sizeof(_FW_CPrivNewHelper*),
  86.             // New helper stack size in bytes.
  87.  
  88.         kSubHelperArraySize = 100,
  89.             // Maximum number of simultaneously tracked subobjects.
  90.             // Subobjects are only tracked until their parent object's constructor
  91.             //   has fully executed.
  92.         kSubHelperArrayByteSize = kSubHelperArraySize*sizeof(_FW_CPrivSubHelper)
  93.             // Subobject helper array size in bytes.
  94.     };
  95.     
  96.     _FW_CPrivNewHelper(__FW_OperatorNewHandler newHandler,
  97.                       __FW_OperatorDeleteHandler deleteHandler);
  98.     ~_FW_CPrivNewHelper();
  99.     
  100.     _FW_CAutoDestructObject *WatchObject(_FW_CAutoDestructObject *watchedObject,
  101.                                        size_t watchedSize) const;
  102.         // Watch watchedObject, return previous watched object.
  103.         // This method is not really const, 
  104.         // but it reduces unnecessary warnings to declare it so.
  105.     
  106.     _FW_CAutoDestructObject *ForgetObject() const;
  107.         // Stop watching any object.
  108.         // This method is not really const, 
  109.         // but it reduces unnecessary warnings to declare it so.
  110.     
  111.     void DeleteWatchedObject();
  112.         // Delete the watched object, including subojects of partially constructed class.
  113.     
  114.     void UpdateForEndConstructor(_FW_CAutoDestructObject *object, size_t size);
  115.         // This method is called only from the FW_END_CONSTRUCTOR macro.
  116.  
  117.     void PushNewHelper();
  118.         // Push this helper onto the helper stack.
  119.         
  120.     short IsWatching(_FW_CAutoDestructObject *object) const;
  121.         // Return 1 if this new helper is tracking object
  122.         
  123.     static short IsSubObject(void* subObject, void* object, size_t objectSize);
  124.  
  125.     static void Initialize(FW_SPrivExceptionGlobals& globals);
  126.         // Initialize the NewHelper and SubHelper modules
  127.         
  128.     static void Terminate();
  129.         // Terminate (cleanup) the NewHelper and SubHelper modules
  130.         
  131.     static _FW_CPrivNewHelper *PopNewHelper();
  132.         // Pop top item from helper stack and return pointer to it.
  133.  
  134.     static _FW_CPrivNewHelper *TopNewHelper();
  135.         // Return pointer to top item on helper stack, but don't pop.
  136.     
  137. private:
  138.  
  139.     _FW_CPrivSubHelper* InList(_FW_CAutoDestructObject *subObject);
  140.         // Search list for entry tracking subObject.
  141.         // Returns NULL if no entry exists.
  142.  
  143.     _FW_CAutoDestructObject *fWatchedObject;
  144.         // The object being constructed.
  145.     
  146.     void *fObjectVTable;
  147.         // The objects v table at time of last completed constructor
  148.         
  149.     size_t fWatchedSize;
  150.         // The size of the object being watched.
  151.         // This information is necessary for tracking subobjects.
  152.         
  153.     _FW_CPrivSubHelper    *fSubHelperList;
  154.         // First link for SubHelper tracking subojects nested in this object.
  155.         // Each subhelper tracks one subobject of the fWatchedObject.
  156.  
  157.     __FW_OperatorNewHandler    fNewHandler;
  158.         // The new handler to use to create memory block.
  159.         
  160.     __FW_OperatorDeleteHandler fDeleteHandler;
  161.         // The delete handler to use to delete the memory block if necessary.
  162. };
  163.  
  164. //----------------------------------------------------------------------------------------
  165. //    _FW_CPrivNewHelper::IsSubObject
  166. //----------------------------------------------------------------------------------------
  167.  
  168. inline short _FW_CPrivNewHelper::IsSubObject(void* subObject, void* object, size_t objectSize)
  169. {
  170.     return (subObject>object && (char*)subObject < (char*)object+objectSize);
  171. }
  172.     
  173. //----------------------------------------------------------------------------------------
  174. //    _FW_CPrivNewHelper::IsWatching
  175. //----------------------------------------------------------------------------------------
  176.  
  177. inline short _FW_CPrivNewHelper::IsWatching(_FW_CAutoDestructObject *object) const
  178. {
  179.     return (object>=fWatchedObject && (char*)object < (char*)fWatchedObject+fWatchedSize);
  180. }
  181.  
  182. #endif
  183.